import QRCode from 'qrcodejs2'
import html2canvas from 'html2canvas'
import Canvas2Image from '../utils/cavas2image.js'
import { base64ImgToOss } from '../utils/imgToOss'
import ClipboardJS from 'clipboard'
import { Toast } from 'vant'

const getShareUrl = (type) => {
  return new Promise((resolve, reject) => {
    // 获取基础域名
    let baseUrl = location.href.split("#")[0]
    baseUrl = baseUrl.substring(0, baseUrl.lastIndexOf('/')+1)
    let shareUrl = baseUrl

    // 是主理人的情况下，才会传userId用于下级注册
    let isBelongMan = JSON.parse(localStorage.getItem("isBelongMan"))
    let userId = localStorage.getItem("userId")
    let shareUserId = isBelongMan ? userId : 0 // 不是主理人默认接口传0

    // 用户中心分享，用于下级注册
    if (!type) {
      shareUrl = `${baseUrl}?shareUserId=${shareUserId}`
    } else

    // ta生活分享
    if(type === 'life'){
      let id = location.href.split("#")[1].split("id=")[1]
      shareUrl = `${baseUrl}?shareUserId=${shareUserId}#/lifeDetail?id=${id}`
    } else

    // 新闻(品牌、门店动态)分享
    if(type === 'news'){
      let id = location.href.split("#")[1].split("id=")[1]
      shareUrl = `${baseUrl}?shareUserId=${shareUserId}#/newsDetail?id=${id}`
    } else

    // 产品
    if (type === "product") {
      let id = location.href.split("#")[1].split("id=")[1]
      shareUrl = `${baseUrl}?shareUserId=${shareUserId}#/product?id=${id}`
    }

    // 课程
    if (type === "class") {
      let id = location.href.split("#")[1].split("id=")[1]
      shareUrl = `${baseUrl}?shareUserId=${shareUserId}#/classDetail?id=${id}`
    }

    // 主理人 or 城市运营商
    if (type === "belong") {
      shareUrl = `${baseUrl}?shareUserId=${shareUserId}&action=invite#/entry`
    }

    // 内部优惠券
    if (type === "coupon") {
      let id = sessionStorage.getItem('shareCouponId')
      if(shareUserId==='699'){
        shareUrl = `${baseUrl}#/coupon?id=${id}`
      } else {
        shareUrl = `${baseUrl}?shareUserId=${shareUserId}#/coupon?id=${id}`
      }
    }
    
    resolve(shareUrl)
  })
  
}

//  生成二维码
const qrcode = (vm, type, size) => {
  return new Promise(async (resolve, reject) => {
    size = size || {width: 54, height: 54}
    let shareUrl = await getShareUrl(type)
    console.log(shareUrl)
    vm.$nextTick(() => {
      new QRCode('qrcode', {
        ...size,
        text: shareUrl
      })
      resolve()
    })
  })
}

const drawHtml = (vm, shareDomQuery, shareRetQuery, type, qrcodeSize) => {
  return new Promise(async (resolve, reject) => {
    await qrcode(vm, type, qrcodeSize)
    setTimeout(() => {
      html2canvas(document.querySelector(shareDomQuery), {
        scale: 2,
        // allowTaint: true,
      })
        .then(async canvas => {
          document.querySelector(shareRetQuery).appendChild(canvas)
          let base64img = Canvas2Image.convertToImage(canvas, canvas.width, canvas.height)
          let imgurl = await base64ImgToOss(vm, base64img, 'mini/develop/share/')
          console.log(imgurl)
          resolve(imgurl)
        })
    }, 1000)
  })
}

const copyInit = async (type) => {
  let shareUrl = await getShareUrl(type)
  console.log('==',shareUrl)
  let clipboard = new ClipboardJS('#copyBtn', {
    text: () => {
      return shareUrl
    }
  })
  clipboard.on('success', e => {
    Toast('链接复制成功, 去分享给好友')
    e.clearSelection()
  })
}

export { drawHtml, copyInit, getShareUrl }